<template>
	<el-container>
		<!-- <el-header>
			<div class="left-panel">
				<div class="module-filter">
					<label style="margin-right: 10px;width: 100px">{{ $t('user.language') }}</label>
					<el-select v-model="params.language" :placeholder="$t('table_data.selectText')" clearable>
						<el-option v-for="item in langList" :key="item.value" :label="item.name" :value="item.value">
						</el-option>
					</el-select>
				</div>
				<div class="module-filter">
					<label style="margin-right: 10px">{{ $t('brand_data.startDate') }}</label>
					<el-date-picker v-model="params.startDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"
						:placeholder="$t('brand_data.startDateLabel')" :disabled-date="beforeOptions"></el-date-picker>
				</div>
				<div class="module-filter">
					<label style="margin-right: 10px">{{ $t('brand_data.endDate') }}</label>
					<el-date-picker v-model="params.endDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"
						:placeholder="$t('brand_data.endDateLabel')" :disabled-date="backOptions"></el-date-picker>
				</div>
				<div class="module-filter btns">
					<el-button type="primary" icon="el-icon-search" @click="upsearch">{{ $t('table_data.searchText') }}</el-button>
					<el-button @click="reset">{{ $t('table_data.resetText') }}</el-button>
				</div>
			</div>
		</el-header> -->
		<el-main class="nopadding">
			<div class="top-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add">{{ $t('table_data.addText') }}</el-button>
			</div>
			<scTable ref="table" :apiObj="apiObj" row-key="id" stripe border :params="sendParams" :hideSetting="true" :sortType="sortDirection" width="100%">
				<el-table-column :label="$t('banner_data.poster')" width="100">
					<template #default="scope">
						<img :src="scope.row.picture" style="width: 72px;object-fit: contain;">
					</template>
				</el-table-column>
				<el-table-column :label="$t('banner_data.name')" prop="bannerName" width="auto"></el-table-column>
				<el-table-column :label="$t('banner_data.sort')" prop="bannerSort" width="auto"></el-table-column>
				<el-table-column :label="$t('banner_data.type')" width="auto">
					<template #default="scope">
						<span>{{ getType(scope.row.type) }}</span>
					</template>
				</el-table-column>
				<el-table-column :label="$t('banner_data.status')" width="auto">
					<template #default="scope">
						<span>{{ getStatus(scope.row.status) }}</span>
					</template>
				</el-table-column>
				<el-table-column :label="$t('table_data.createAt')" prop="createAt" width="auto" sortable></el-table-column>
				<el-table-column :label="$t('table_data.operate')" width="auto">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_detail(scope.row, scope.$index)">{{ $t('table_data.detail') }}</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">{{ $t('table_data.edit') }}</el-button>
							<el-button text :type="scope.row.status==1||scope.row.status==3?'success':'danger'" size="small" @click="table_opera(scope.row)">{{ scope.row.status==1||scope.row.status==3?$t('banner_data.upload'):$t('banner_data.remove') }}</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
</template>

<script>
import saveDialog from './client/save'

export default {
	name: "client",
	components: {
		saveDialog
	},
	data() {
		return {
			direction: 'direction',
			statusList: [
				{
					name: this.$t('banner_data.status1'),
					value: 1,
				},
				{
					name: this.$t('banner_data.status2'),
					value: 2,
				},
				{
					name: this.$t('banner_data.status3'),
					value: 3,
				}
			],
			classList: [
				{
					name: this.$t('banner_data.class1'),
					value: 1,
				},
				{
					name: this.$t('banner_data.class2'),
					value: 2,
				},
				{
					name: this.$t('banner_data.class3'),
					value: 3,
				},
				{
					name: this.$t('banner_data.class4'),
					value: 4,
				},
				{
					name: this.$t('banner_data.class5'),
					value: 5,
				},
				{
					name: this.$t('banner_data.class6'),
					value: 6,
				},
				{
					name: this.$t('banner_data.class7'),
					value: 7,
				}
			],
			tonghList: [
				{
					name: this.$t('table_data.currency.dollar'),
					value: 2,
				},
				{
					name: this.$t('table_data.currency.rmb'),
					value: 3,
				},
				{
					name: this.$t('table_data.currency.jpy'),
					value: 1,
				}
			],
			movieData: [

			],
			theatreData: [

			],
			dialog: {
				save: false
			},
			search: {
				keyword: ""
			},
			apiObj: {},
			selection: [],
			searchKey: null,
			params: {
				// currency: null,
				// movieId: null,
				// category: null,
				startDate: null,
				endDate: null,
				language: null
			},
			sendParams: {

			}
		}
	},
	computed: {
		getType(){
			return (e)=>{
				return this.classList.find(n=>n.value == e).name
			}
		},
		getStatus(){
			return (e)=>{
				return this.statusList.find(n=>n.value == e).name
			}
		}
	},
	async created() {
		this.apiObj = this.$API.banner.getBanner;
		
	},
	methods: {
		closeTag(){
			const tagList = this.$store.state.viewTags.viewTags;
			const tag = this.$route;
			const nowTagIndex = tagList.findIndex(item => item.fullPath == tag.fullPath)
			this.$store.commit("removeViewTags", tag)
			this.$store.commit("removeIframeList", tag)
			this.$store.commit("removeKeepLive", tag.name)

			const leftView = tagList[nowTagIndex - 1]
			if (leftView) {
				this.$router.push(leftView)
			} else {
				this.$router.push('/')
			}
		},
		reset() {
			// this.params.currency = null;
			// this.params.movieId = null;
			// this.params.category = null;
			this.params.startDate = null;
			this.params.endDate = null;
			this.params.language = null;
			this.upsearch();
			// this.$refs.table.upData(this.params);
		},
		upsearch() {
			Object.assign(this.sendParams,this.params);
			if(this.sendParams.startDate)this.sendParams.startDate = this.$TOOL.getLocalTime(9,this.params.startDate,true)//this.$TOOL.dateFormat(this.sendParams.startDate);
			if(this.sendParams.endDate)this.sendParams.endDate = this.$TOOL.getLocalTime(9,this.params.endDate,true)//this.$TOOL.dateFormat(this.sendParams.endDate);
			this.$refs.table.upData(this.sendParams);
		},
		//增加
		add() {
			this.$router.push({path:'/bannerTable/client/save',query:{type:'add','id':'add'}})
			// this.dialog.save = true
			// this.$nextTick(() => {
			// 	this.$refs.saveDialog.open()
			// })
		},
		//编辑
		table_edit(row){
			this.$router.push({path:'/bannerTable/client/save',query:{type:'edit','id':row.bannerId}})
			// this.dialog.save = true
			// this.$nextTick(() => {
			// 	this.$refs.saveDialog.open('edit').setData(row)
			// })
		},
		// 详情
		table_detail(row){
			this.$router.push({path:'/bannerTable/client/save',query:{type:'detail','id':row.bannerId}})
			// this.dialog.save = true
			// this.$nextTick(() => {
			// 	this.$refs.saveDialog.open('edit').setData(row)
			// })
		},
		// 上下架操作
		async table_opera(row) {
			this.$confirm(this.$t('banner_data.deltips1',{type: row.status==1?this.$t('banner_data.upload'):this.$t('banner_data.remove')}), this.$t('table_data.tips'), {
				confirmButtonText: this.$t('table_data.editText'),
				cancelButtonText: this.$t('table_data.cancelText'),
				type: 'warning'
			}).then(async () => {
				var res = await this.$API.banner.setStatus.post({bannerId: row.bannerId});
				if(res.code == 'OK'){
					//这里选择刷新整个表格 OR 插入/编辑现有表格数据
					this.$refs.table.upData(this.params);
					this.$message.success(this.$t('table_data.delsucc'))
				}else{
					this.$alert(res.message, this.$t('table_data.tips'), {type: 'error'})
				}
			}).catch(() => {
				// this.$message({
				// 	type: 'info',
				// 	message: 'cancel'
				// });          
			});
		},
		//批量删除
		async batch_del() {
			this.$confirm(`${this.$t('table_data.deltips1')} ${this.selection.length} ${this.$t('table_data.deltips2')}`, this.$t('table_data.tips'), {
				type: 'warning'
			}).then(() => {
				const loading = this.$loading();
				this.selection.forEach(item => {
					this.$refs.table.tableData.forEach((itemI, indexI) => {
						if (item.id === itemI.id) {
							this.$refs.table.tableData.splice(indexI, 1)
						}
					})
				})
				loading.close();
				this.$message.success(this.$t('table_data.operasucc'))
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				data.id = new Date().getTime()
				this.$refs.table.tableData.unshift(data)
			} else if (mode == 'edit') {
				this.$refs.table.tableData.filter(item => item.id === data.id).forEach(item => {
					Object.assign(item, data)
				})
			}
		},
		beforeOptions(v){
			if(!this.params.endDate)return false
			return v.getTime() > new Date(this.params.endDate).getTime();//  - 86400000是否包括当天
		},
		backOptions(v){
			return v.getTime() < new Date(this.params.startDate).getTime();//  - 86400000是否包括当天
		},
	}
}
</script>

<style scoped>
.module-filter {
	margin-right: 10px;
}

.el-header {
	--el-header-height: auto;
}

.left-panel {
	flex-wrap: wrap;
	line-height: 50px;
}

.top-panel {
	margin: 10px;
}

.scTable {
	height: calc(100% - 60px) !important;
}

.el-main.nopadding {
	margin: 0 10px;
}

.el-header {
	margin: 10px;
	border-radius: 4px;
}

@media only screen and (max-width: 820px) and (min-width: 768px) {
	.module-filter {
    flex: 0 0 30%;
    display: flex;
    flex-direction: column;
    margin: 0 10px;
	}
	.btns {
    margin: 10px;
    width: 340px;
    flex-direction: row;
	}
}
</style>
